import axios from '../config/axiosConfig'

export const queryCustomMenus = function () {
  return new Promise((resolve, reject)=>{
    axios.get(MENUUSER_SERVICE_URL + 'customMenu/queryCustomMenus'
    ).then((res) => {
      let data = res.data;
      resolve(data);
    })
  });
};

export const queryMenus = ()=>{
  return new Promise(function(resolve, reject) {
    axios.get(MENUCORE_SERVICE_URL + 'indexMenu/queryMenus'
    ).then((res) => {
      let data = res.data;
      if (data.status === 'SUCCESS') {
        resolve(data.body);
        // allMenus = data.body;
      } else {
        resolve('');
        console.error(data.body);
        window.location.href = UAP_SERVER_NAME + 'error.html';
      }
    })
  });
};

export const queryMenuHotKeys = ()=>{
  return new Promise(function(resolve, reject) {
    axios.get(CUSTOM_SERVICE_URL + 'menuHotKey/queryMenuHotKeys')
      .then(function (res) {
        let  data = res.data;
      if (data.status == 'SUCCESS') {
        $.each(data.body, function (index, item) {
          asciiUumsFunction[item.menuHotKey.keyboardAscii] = item.uumsFunction;
          var $treeControl = $('#' + toggleKeyNameOrAscii(item.menuHotKey.keyboardAscii)).next().find(".tree-control");
          $treeControl.find('span').html(item.uumsFunction.functionName);

          //解绑事件
          drawHotKeyInfo({
            "functionName": item.uumsFunction.functionName,
            "functionCode": item.uumsFunction.functionCode,
            "keyboardAscii": item.menuHotKey.keyboardAscii
          }, $treeControl);
        });
        $(window).keydown(function (event) {
          if (asciiUumsFunction[event.keyCode]) {
            openTab(
              asciiUumsFunction[event.keyCode].functionName,
              asciiUumsFunction[event.keyCode].functionUrl,
              asciiUumsFunction[event.keyCode].pageEmbedType,
              asciiUumsFunction[event.keyCode].functionCode,
              asciiUumsFunction[event.keyCode].sourceSystem,
              asciiUumsFunction[event.keyCode].parentFunction,
              HOT_KEY_ACCESS
            );

            event.preventDefault(); // 阻止按键原功能生效
          }
        });
      }
    })
  });
};

const bindMenuHotKey = (menuCode, keyboardAscii, successCallback)=>{
  axios.post(CUSTOM_SERVICE_URL + "menuHotKey/bindMenuHotKey", {
    menuCode: menuCode,
    keyboardAscii: keyboardAscii
  }).then((res)=>{
    let data = res.data;
    if (data.status === 'SUCCESS') {
      if (successCallback) {
        successCallback(data.body);
      }
    } else if (data.status === 'FAILURE') {
      Ext.Msg.alert("操作失败 !", data.body);
    }
  })
};

const unbindMenuHotKey = (keyboardAscii, successCallback)=>{
  axios.post(CUSTOM_SERVICE_URL + "menuHotKey/unbindMenuHotKey", {
    keyboardAscii: keyboardAscii
  }).then((res)=>{
    let data = res.data;
    if (data.status === "SUCCESS") {
      if (successCallback) {
        successCallback(data.body);
      }
    } else if (data.status === 'FAILURE') {
      Ext.Msg.alert("操作失败 !", data.body);
    }
  })
};
